#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "vec.h"
#include <time.h>

#define DEBUG 1

#define FIFO_SERVER "/tmp/sope-fifo"
#define FIFO_MAX_OPEN_TRIES 10
#define FIFO_MAX_MSG 512

#define BUFFER_MAX 256 

#define SUCCESS 0
#define ERROR 1

enum operations { WITHDRAWL, DEPOSIT, TRANS, CONSULT, CREATE_U, DELETE_U, LIST_U, SHUTDOWN, LOGIN, OK, ERROR_};
enum programType { SERVER, ADMIN, CLIENT};

typedef struct {
	unsigned int id;
	char name[20];
	char pin[4];
	double balance;
} user;

typedef struct request
{
	int pid;
	char type;
	int AccountNumber;
	char AccountName[21];
	char pin[5];
	float AccountBalance;
	float value; // For transfers
	int TargetAccount; // For transfers
}Request;

int createFifo(char *name, mode_t mode);
void readFifo(int fifo, char *str);
int Open_Fifo(int pid);

vec* loadUsers(char *filename, unsigned int *lastID);
user* getUser(vec * users, unsigned id);
int addUser(vec * users, unsigned int *lastID, char *name, char *pin);
int removeUser(vec * users, unsigned int id);
int saveUsers(vec * users, char *filename, unsigned int lastID);
void printUsers(vec * users);
double setBalance(user *, double amount);

int readline(int fd, char *str);

void sortUsers(vec *users);
static int compareUsers(const void *u1, const void *u2);

void CleanStdin(void);

